﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Xml;
using System.IO;

namespace JQueryEasyUI
{
   public class EasyUIXmlReader
    {
       public static string  filename=Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)+"\\JQueryEasyUI.xml";
       private static string lastWriteTime = "";
           private static XmlDocument doc = null;
       /// <summary>
       /// 获取对某一列的样式配置。
       /// </summary>
       /// <param name="field"></param>
       /// <param name="title"></param>
       /// <returns></returns>
       public static Hashtable GetDataGridColumnStyle(string field, string title, string fieldclass, out bool isFrozenColumn)
       {
           LoadXml();
           Hashtable hash = new Hashtable();
           isFrozenColumn=false;
           string xpath = string.Format("easyui/datagrid/columns/applys/apply[@field=\"{0}\" and @title=\"{1}\" and @fieldclass=\"{2}\"]", field, title, fieldclass);
          
           XmlNode node = doc.SelectSingleNode(xpath);
           if(node!=null)
           {
            string boolvalue=node.Attributes["isfrozencolumn"].Value;
            string stylevalue = node.Attributes["linkstyle"].Value;
            if (!string.IsNullOrEmpty(stylevalue))
            {
                string[] styles = stylevalue.Split(new char[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries);
                hash = CompositColumnStyles(styles);
            }
            if(boolvalue.Trim().ToLower()=="true")isFrozenColumn=true;
           }

           return hash;
       }

       /// <summary>
       /// 组合样式
       /// </summary>
       /// <param name="styels"></param>
       /// <returns></returns>
       private static Hashtable CompositColumnStyles(string[] styels)
       {
           Hashtable hash = new Hashtable();
           int len = styels.Length;
           for (int i = len - 1; i>=0; i--)
           {
               XmlNode node = doc.SelectSingleNode(string.Format("easyui/datagrid/columns/propertiesstyles/style[@name=\"{0}\"]", styels[i]));
               if (node != null)
               {
                   foreach (XmlNode x in node.ChildNodes)
                   {
                       if (!hash.ContainsKey(x.Name))
                       {
                           hash.Add(x.Name, x.InnerText);
                       }
                   }
                  

               }
           }
          
           return hash;
           
       }

       public static void LoadXml()
       {
           string ss=File.GetLastWriteTime(filename).ToString();
           if (doc == null || lastWriteTime!=ss)
           {

               if (File.Exists(filename))
               {
                   doc = new XmlDocument();
                   try
                   {
                       doc.LoadXml(File.ReadAllText(filename));
                       lastWriteTime = ss;
                   }
                   catch (Exception){
                       doc = null;
                   }
               }
               else {
                   throw new FileNotFoundException();
               }
 
           }
       }
    }
}
